//
//  CSDatePickerView.m
//  市场监管通
//
//  Created by 网新中研 on 16/5/31.
//  Copyright © 2016年 centralsoft. All rights reserved.
//

#import "CSDatePickerView.h"
#import "HooDatePicker.h"
#import "KLCPopup.h"
#import "Common.h"

#define BUTTON_WIDTH 90.0f

@interface CSDatePickerView()<HooDatePickerDelegate>

@property (nonatomic, strong) HooDatePicker *datePicker;

@property (nonatomic, copy) SaveClickBlock saveClickHandler;
@property (nonatomic, copy) CancelClickBlock cancelClickHander;

@property (nonatomic, strong) UIButton *saveButton;

/**
 * 清空按钮
 */
@property (nonatomic, strong) UIButton *clearButton;

@property (nonatomic, strong) UIView *pickerBackView;

@end

@implementation CSDatePickerView

static CSDatePickerView *_dateTimePickView;

/**
 *  单例对象
 *
 *  @return DateTimePickView 对象
 */
+(instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _dateTimePickView = [[CSDatePickerView alloc]initWithFrame:CGRectMake(0, 0, DATE_TIME_PICKER_VIEW_WIDTH, DATE_TIME_PICKER_VIEW_HEIGHT)];
        
    });
    return _dateTimePickView;
}

+ (CSDatePickerView *)instanceDateTimePickViewWithSaveClickHandler:(SaveClickBlock)saveClickHandler
                                                cancelClickHandler:(CancelClickBlock)cancelClickHander{
    CSDatePickerView *datePickerView = [CSDatePickerView sharedInstance];
    datePickerView.saveClickHandler = saveClickHandler;
    datePickerView.cancelClickHander = cancelClickHander;
    return datePickerView;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        
        self.backgroundColor = [UIColor whiteColor];
        
        self.pickerBackView = [[UIView alloc]initWithFrame:CGRectMake(10, 15, 240, 200)];
        self.pickerBackView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.pickerBackView];
        
        self.datePicker = [[HooDatePicker alloc]initWithSuperView:self.pickerBackView];
        self.datePicker.delegate = _dateTimePickView;
        self.datePicker.datePickerMode = HooDatePickerModeDate;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
        NSDate *maxDate = [dateFormatter dateFromString:@"01-01-2100 00:00:00"];
        NSDate *minDate = [dateFormatter dateFromString:@"01-01-1990 00:00:00"];
        
        [self.datePicker setDate:[NSDate date] animated:YES];
        self.datePicker.minimumDate = minDate;
        self.datePicker.maximumDate = maxDate;
        
        [self.datePicker show];
        
        [self addSubview:self.saveButton];
        
        [self addSubview:self.clearButton];
    }
    return self;
}


-(void)saveClick{
    if(self.saveClickHandler != nil){
        NSDate *selected = [self.datePicker date];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString *currentOlderOneDateStr = [dateFormatter stringFromDate:selected];
        self.saveClickHandler(currentOlderOneDateStr);
    }
    
    if(self.superKLCPopup != nil){
        [self.superKLCPopup dismiss:YES];
    }
}

-(void)clearClick{
    if(self.saveClickHandler != nil){
        self.saveClickHandler(@"");
    }
    
    if(self.superKLCPopup != nil){
        [self.superKLCPopup dismiss:YES];
    }
}

#pragma mark - HooDatePickerDelegate

- (void)datePicker:(HooDatePicker *)datePicker dateDidChange:(NSDate *)date {
    
    NSLog(@"- (void)datePicker:(HooDatePicker *)datePicker dateDidChange:(NSDate *)date: %@", date);
    
}

- (void)datePicker:(HooDatePicker *)datePicker didCancel:(UIButton *)sender {
    
    NSLog(@"- (void)datePicker:(HooDatePicker *)datePicker didCancel:(UIButton *)sender");
}

- (void)datePicker:(HooDatePicker *)datePicker didSelectedDate:(NSDate*)date {
    
    NSLog(@"- (void)datePicker:(HooDatePicker *)datePicker didSelectedDate:(NSDate*)date");
    
}

-(UIButton *)saveButton{
    if (_saveButton == nil) {
        
        CGFloat largeSize = 0.0;
        
        _saveButton = [[UIButton alloc]initWithFrame:CGRectMake(DATE_TIME_PICKER_VIEW_WIDTH / 2.0 - (BUTTON_WIDTH + BUTTON_WIDTH)/2.0  + BUTTON_WIDTH - largeSize, 225, BUTTON_WIDTH + largeSize, 28)];
        [_saveButton setTitle:@"确定" forState:UIControlStateNormal];
        STYLE_BUTTON_LOCATION(_saveButton);
        _saveButton.layer.cornerRadius = 0;
        _saveButton.layer.borderWidth = 0;
        _saveButton.titleLabel.font = [UIFont boldFlatFontOfSize:18.0];
        [_saveButton addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveButton;
}

-(UIButton *)clearButton{
    if (_clearButton == nil) {
        CGFloat largeSize = 0.0;
        _clearButton = [[UIButton alloc]initWithFrame:CGRectMake(DATE_TIME_PICKER_VIEW_WIDTH / 2.0 - (BUTTON_WIDTH+ BUTTON_WIDTH)/2.0, 225, BUTTON_WIDTH - largeSize + 1, 28)];
        [_clearButton setTitle:@"清空" forState:UIControlStateNormal];
        STYLE_BUTTON_LOCATION(_clearButton);
        _clearButton.titleLabel.font = [UIFont systemFontOfSize: 16.0];
        _clearButton.layer.cornerRadius = 0;
        //        _clearButton.layer.cornerRadius = 5;
        _clearButton.layer.borderWidth = 0;
        [_clearButton addTarget:self action:@selector(clearClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _clearButton;
}

@end
